package com.cldz.service;

import com.cldz.common.dto.res.FsuDataResDto;
import com.cldz.utils.BeanCopyUtil;
import com.cldz.dao.FsuDataMapper;
import com.cldz.common.dto.req.FsuDataDto;
import com.cldz.entity.FsuData;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ Author     ：lixiao.
 * @ Date       ：Created in 10:18 2018/12/22
 * @ Description：
 * @ Modified By：
 */
@Service("fsuDataService")
public class FsuDataService {

    @Autowired
    private FsuDataMapper fsuDataMapper;

    public List<FsuDataResDto> getFsuDeviceHisData(FsuDataDto fsuDataDto){
        spAm(fsuDataDto);

        List<FsuData> fsuDeviceHisData = fsuDataMapper.getFsuDeviceHisData(fsuDataDto);
        return copyListBean(fsuDeviceHisData);
    }

    public Integer getFsuDeviceHisDataAmount(FsuDataDto fsuDataDto){
        spAm(fsuDataDto);
        return fsuDataMapper.getFsuDeviceHisDataAmount(fsuDataDto);
    }

    public List<FsuDataResDto> getFsuDeviceRealData(FsuDataDto fsuDataDto){
        List<FsuData> fsuDeviceHisData = fsuDataMapper.getFsuDeviceRealData(fsuDataDto);
        return copyListBean(fsuDeviceHisData);
    }

    public Integer getFsuDeviceRealDataAmount(FsuDataDto fsuDataDto){
        return fsuDataMapper.getFsuDeviceRealDataAmount(fsuDataDto);
    }

    private void spAm(FsuDataDto fsuDataDto) {
        if (StringUtils.isNotBlank(fsuDataDto.getDeviceIDs())) {
            fsuDataDto.setDeviceIDs(fsuDataDto.getDeviceIDs().replaceAll(",", "','"));
        }

        if (StringUtils.isNotBlank(fsuDataDto.getSpIDs())) {
            fsuDataDto.setSpIDs(fsuDataDto.getSpIDs().replaceAll(",", "','"));
        }
    }

    private List<FsuDataResDto> copyListBean(List<FsuData> devListByTypeID) {
        List<FsuDataResDto> list = new ArrayList<>();
        for (FsuData device : devListByTypeID) {
            FsuDataResDto dto = new FsuDataResDto();
            BeanCopyUtil.copyPropertiesIgnoreNull(device, dto);
//            BeanUtils.copyProperties(device, dto);
            list.add(dto);
        }
        return list;
    }

}
